<?php
App::uses('AppModel', 'Model');
/**
 * Fee Model
 */
class BankBatch extends AppModel {

    public function save($data = null,$validate = true, $fieldList = array()) {
        $s = $this->sanitize($data);
        return parent::save($s);
    }

    function getBatchNo($bank_id,$type='internal'){
        //We used to get next auto increment as batch no, but not any more
        $data = $this->find('first',array(
            'conditions'=>array('BankBatch.bank_id'=>$bank_id),
            'recursive'=>-1
        ));
        if($data){
            $num = $data['BankBatch']['internal_next_batch_no'];
            if($type == 'external'){
                $num = $data['BankBatch']['external_next_batch_no'];
            }
            $num_length = strlen((string)$num);
            if($num_length < 2){
                $diff = 2 - $num_length;
                $zeros = '';
                for($x=1; $x <= $diff; $x++){
                    $zeros .= '0';
                }
                $new_num = $zeros.$num;
            }
            else{
                $new_num = $num;
            }

           return array(
               'id'=>$data['BankBatch']['id'],
               'next_batch_no'=>$new_num
           );
        }
        else{
            return false;
        }
    }

    function setBatchNo($id,$batch_no,$type='internal'){
        $save = array(
            'id'=>$id,
            'internal_next_batch_no'=>$batch_no
        );
        if($type == 'external'){
            $save = array(
                'id'=>$id,
                'external_next_batch_no'=>$batch_no
            );
        }
        $data = $this->save($save);
        if($data){
            return true;
        }
        else{
            return false;
        }
    }
}